package com.tf_cut.backend.agent.service;

import com.tf_cut.backend.agent.pojo.AgentCashOrder;
import com.tf_cut.backend.agent.repository.AgentCashOrderRepository;
import com.tf_cut.backend.agent.vo.AgentCashOrderVo;
import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
@Transactional
public class AgentCashOrderServiceImpl extends CommonServiceImpl<AgentCashOrderVo, AgentCashOrder, String> implements  AgentCashOrderService{

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private AgentCashOrderRepository agentCashOrderRepository;

    @Autowired
    private com.tf_cut.backend.mapper.AgentCashMapper agentCashMapper;

    @Override
    public List<AgentCashOrderVo> findlistCashOrderpage(AgentCashOrderVo avo) {
        List<AgentCashOrderVo> listVo = agentCashMapper.findlistCashOrder(avo);
        return  listVo;

    }

    @Override
    public String getTXSerialNo() {
        return agentCashMapper.getTXSerialNo();
    }

    @Override
    public void updateAgentCashOrder(AgentCashOrderVo agentCashOrderVo) {
        if(agentCashOrderVo.getOrderType() == 1) {
            agentCashMapper.insAgentDetails(agentCashOrderVo);
        }else{
            //取消审核退回金额
            agentCashMapper.updateAgentAccount1(agentCashOrderVo);
        }
        agentCashMapper.updateAgentCashOrder(agentCashOrderVo);
    }

    @Override
    public PageInfo<AgentCashOrderVo> findListAgentCashOrder(AgentCashOrderVo avo) {
        com.github.pagehelper.PageHelper.startPage(avo.getPage(),avo.getRows());
        List<AgentCashOrderVo> list = agentCashMapper.findlistCashOrder(avo);
        com.github.pagehelper.PageInfo<AgentCashOrderVo> pageinfo = new com.github.pagehelper.PageInfo<>(list);

        PageInfo<AgentCashOrderVo> returnPageInfo = new PageInfo<>();
        returnPageInfo.setPage(pageinfo.getPageNum());//页码
        returnPageInfo.setPageSize(pageinfo.getPageSize());//页面大小
        returnPageInfo.setRows(pageinfo.getList());//分页结果
        returnPageInfo.setRecords((int)pageinfo.getTotal());//总记录数
        returnPageInfo.setTotal(pageinfo.getPages());//总页数
        return returnPageInfo;
    }


    public void update(AgentCashOrderVo agentCashOrderVo){
        agentCashMapper.updateAgentAccount(agentCashOrderVo);
    }

    public String qryLastSerial(String orderId){
        return agentCashMapper.qryLastSerial(orderId);
    }
}
